home *** CD-ROM | disk | FTP | other *** search
/ Qoole for Quake / Qoole for Quake (USA) / Qoole for Quake (USA).bin / Tutorial / HTML / QUBE.ZIP / SRC / LTFACE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-05  |  12.4 KB  |  597 lines

  1.  
  2. #include "light.h"
  3. #include "curs.h"
  4.  
  5. /*
  6. ============
  7. CastRay
  8.  
  9. Returns the distance between the points, or -1 if blocked
  10. =============
  11. */
  12. double CastRay (vec3_t p1, vec3_t p2)
  13. {
  14.     int        i;
  15.     double    t;
  16.     qboolean    trace;
  17.         
  18.     if (!fastlight) {
  19.         trace = TestLine (p1, p2);
  20.  
  21.         if (!trace)
  22.             return -1;        /* ray was blocked */
  23.     }
  24.                 
  25.     t = 0;
  26.     for (i=0 ; i< 3 ; i++)
  27.         t += (p2[i]-p1[i]) * (p2[i]-p1[i]);
  28.         
  29.     if (t == 0)
  30.         t = 1;        /* don't blow up... */
  31.     return sqrt(t);
  32. }
  33.  
  34. /*
  35. ===============================================================================
  36.  
  37. SAMPLE POINT DETERMINATION
  38.  
  39. void SetupBlock (dface_t *f) Returns with surfpt[] set
  40.  
  41. This is a little tricky because the lightmap covers more area than the face.
  42. If done in the straightforward fashion, some of the
  43. sample points will be inside walls or on the other side of walls, causing
  44. false shadows and light bleeds.
  45.  
  46. To solve this, I only consider a sample point valid if a line can be drawn
  47. between it and the exact midpoint of the face.  If invalid, it is adjusted
  48. towards the center until it is valid.
  49.  
  50. (this doesn't completely work)
  51.  
  52. ===============================================================================
  53. */
  54.  
  55. #define    SINGLEMAP    (18*18*4)
  56.  
  57. typedef struct
  58. {
  59.     double    lightmaps[MAXLIGHTMAPS][SINGLEMAP];
  60.     int        numlightstyles;
  61.     double    *light;
  62.     double    facedist;
  63.     vec3_t    facenormal;
  64.  
  65.     int        numsurfpt;
  66.     vec3_t    surfpt[SINGLEMAP];
  67.  
  68.     vec3_t    texorg;
  69.     vec3_t    worldtotex[2];    /* s = (world - texorg) . worldtotex[0] */
  70.     vec3_t    textoworld[2];    /* world = texorg + s * textoworld[0] */
  71.  
  72.     double    exactmins[2], exactmaxs[2];
  73.     
  74.     int        texmins[2], texsize[2];
  75.     int        lightstyles[256];
  76.     int        surfnum;
  77.     dface_t    *face;
  78. } lightinfo_t;
  79.  
  80.  
  81. /*
  82. ================
  83. CalcFaceVectors
  84.  
  85. Fills in texorg, worldtotex. and textoworld
  86. ================
  87. */
  88. void CalcFaceVectors (lightinfo_t *l)
  89. {
  90.     texinfo_t    *tex;
  91.     int            i, j;
  92.     vec3_t    texnormal;
  93.     float    distscale;
  94.     double    dist, len;
  95.  
  96.     tex = &texinfo[l->face->texinfo];
  97.     
  98. /* convert from float to double */
  99.     for (i=0 ; i<2 ; i++)
  100.         for (j=0 ; j<3 ; j++)
  101.             l->worldtotex[i][j] = tex->vecs[i][j];
  102.  
  103. /* calculate a normal to the texture axis.  points can be moved along this */
  104. /* without changing their S/T */
  105.     texnormal[0] = tex->vecs[1][1]*tex->vecs[0][2]
  106.         - tex->vecs[1][2]*tex->vecs[0][1];
  107.     texnormal[1] = tex->vecs[1][2]*tex->vecs[0][0]
  108.         - tex->vecs[1][0]*tex->vecs[0][2];
  109.     texnormal[2] = tex->vecs[1][0]*tex->vecs[0][1]
  110.         - tex->vecs[1][1]*tex->vecs[0][0];
  111.     VectorNormalize (texnormal);
  112.  
  113. /* flip it towards plane normal */
  114.     distscale = DotProduct (texnormal, l->facenormal);
  115.     if (!distscale)
  116.         Error ("Texture axis perpendicular to face");
  117.     if (distscale < 0)
  118.     {
  119.         distscale = -distscale;
  120.         VectorSubtract (vec3_origin, texnormal, texnormal);
  121.     }    
  122.  
  123. /* distscale is the ratio of the distance along the texture normal to */
  124. /* the distance along the plane normal */
  125.     distscale = 1/distscale;
  126.  
  127.     for (i=0 ; i<2 ; i++)
  128.     {
  129.         len = VectorLength (l->worldtotex[i]);
  130.         dist = DotProduct (l->worldtotex[i], l->facenormal);
  131.         dist *= distscale;
  132.         VectorMA (l->worldtotex[i], -dist, texnormal, l->textoworld[i]);
  133.         VectorScale (l->textoworld[i], (1/len)*(1/len), l->textoworld[i]);
  134.     }
  135.  
  136.  
  137. /* calculate texorg on the texture plane */
  138.     for (i=0 ; i<3 ; i++)
  139.         l->texorg[i] = -tex->vecs[0][3]* l->textoworld[0][i] - tex->vecs[1][3] * l->textoworld[1][i];
  140.  
  141. /* project back to the face plane */
  142.     dist = DotProduct (l->texorg, l->facenormal) - l->facedist - 1;
  143.     dist *= distscale;
  144.     VectorMA (l->texorg, -dist, texnormal, l->texorg);
  145.     
  146. }
  147.  
  148. /*
  149. ================
  150. CalcFaceExtents
  151.  
  152. Fills in s->texmins[] and s->texsize[]
  153. also sets exactmins[] and exactmaxs[]
  154. ================
  155. */
  156. void CalcFaceExtents (lightinfo_t *l)
  157. {
  158.     dface_t *s;
  159.     double    mins[2], maxs[2], val;
  160.     int        i,j, e;
  161.     dvertex_t    *v;
  162.     texinfo_t    *tex;
  163.     
  164.     s = l->face;
  165.  
  166.     mins[0] = mins[1] = 999999;
  167.     maxs[0] = maxs[1] = -99999;
  168.  
  169.     tex = &texinfo[s->texinfo];
  170.     
  171.     for (i=0 ; i<s->numedges ; i++)
  172.     {
  173.         e = dsurfedges[s->firstedge+i];
  174.         if (e >= 0)
  175.             v = dvertexes + dedges[e].v[0];
  176.         else
  177.             v = dvertexes + dedges[-e].v[1];
  178.         
  179.         for (j=0 ; j<2 ; j++)
  180.         {
  181.             val = v->point[0] * tex->vecs[j][0] + 
  182.                 v->point[1] * tex->vecs[j][1] +
  183.                 v->point[2] * tex->vecs[j][2] +
  184.                 tex->vecs[j][3];
  185.             if (val < mins[j])
  186.                 mins[j] = val;
  187.             if (val > maxs[j])
  188.                 maxs[j] = val;
  189.         }
  190.     }
  191.  
  192.     for (i=0 ; i<2 ; i++)
  193.     {    
  194.         l->exactmins[i] = mins[i];
  195.         l->exactmaxs[i] = maxs[i];
  196.         
  197.         mins[i] = floor(mins[i]/16);
  198.         maxs[i] = ceil(maxs[i]/16);
  199.  
  200.         l->texmins[i] = mins[i];
  201.         l->texsize[i] = maxs[i] - mins[i];
  202.         if (l->texsize[i] > 17)
  203.             Error ("Bad surface extents");
  204.     }
  205. }
  206.  
  207. /*
  208. =================
  209. CalcPoints
  210.  
  211. For each texture aligned grid point, back project onto the plane
  212. to get the world xyz value of the sample point
  213. =================
  214. */
  215. int c_bad;
  216. void CalcPoints (lightinfo_t *l)
  217. {
  218.     int        i;
  219.     int        s, t, j;
  220.     int        w, h, step;
  221.     double    starts, startt, us, ut;
  222.     double    *surf;
  223.     double    mids, midt;
  224.     vec3_t    facemid, move;
  225.  
  226. /* */
  227. /* fill in surforg */
  228. /* the points are biased towards the center of the surface */
  229. /* to help avoid edge cases just inside walls */
  230. /* */
  231.     surf = l->surfpt[0];
  232.     mids = (l->exactmaxs[0] + l->exactmins[0])/2;
  233.     midt = (l->exactmaxs[1] + l->exactmins[1])/2;
  234.  
  235.     for (j=0 ; j<3 ; j++)
  236.         facemid[j] = l->texorg[j] + l->textoworld[0][j]*mids + l->textoworld[1][j]*midt;
  237.  
  238.     if (extrasamples)
  239.     {    /* extra filtering */
  240.         h = (l->texsize[1]+1)*2;
  241.         w = (l->texsize[0]+1)*2;
  242.         starts = (l->texmins[0]-0.5)*16;
  243.         startt = (l->texmins[1]-0.5)*16;
  244.         step = 8;
  245.     }
  246.     else
  247.     {
  248.         h = l->texsize[1]+1;
  249.         w = l->texsize[0]+1;
  250.         starts = l->texmins[0]*16;
  251.         startt = l->texmins[1]*16;
  252.         step = 16;
  253.     }
  254.  
  255.     l->numsurfpt = w * h;
  256.     for (t=0 ; t<h ; t++)
  257.     {
  258.         for (s=0 ; s<w ; s++, surf+=3)
  259.         {
  260.             us = starts + s*step;
  261.             ut = startt + t*step;
  262.  
  263.         /* if a line can be traced from surf to facemid, the point is good */
  264.             for (i=0 ; i<6 ; i++)
  265.             {
  266.             /* calculate texture point */
  267.                 for (j=0 ; j<3 ; j++)
  268.                     surf[j] = l->texorg[j] + l->textoworld[0][j]*us
  269.                     + l->textoworld[1][j]*ut;
  270.  
  271.                 if (CastRay (facemid, surf) != -1)
  272.                     break;    /* got it */
  273.                 if (i & 1)
  274.                 {
  275.                     if (us > mids)
  276.                     {
  277.                         us -= 8;
  278.                         if (us < mids)
  279.                             us = mids;
  280.                     }
  281.                     else
  282.                     {
  283.                         us += 8;
  284.                         if (us > mids)
  285.                             us = mids;
  286.                     }
  287.                 }
  288.                 else
  289.                 {
  290.                     if (ut > midt)
  291.                     {
  292.                         ut -= 8;
  293.                         if (ut < midt)
  294.                             ut = midt;
  295.                     }
  296.                     else
  297.                     {
  298.                         ut += 8;
  299.                         if (ut > midt)
  300.                             ut = midt;
  301.                     }
  302.                 }
  303.                 
  304.                 /* move surf 8 pixels towards the center */
  305.                 VectorSubtract (facemid, surf, move);
  306.                 VectorNormalize (move);
  307.                 VectorMA (surf, 8, move, surf);
  308.             }
  309.             if (i == 2)
  310.                 c_bad++;
  311.         }
  312.     }
  313.     
  314. }
  315.  
  316.  
  317. /*
  318. ===============================================================================
  319.  
  320. FACE LIGHTING
  321.  
  322. ===============================================================================
  323. */
  324.  
  325. int        c_culldistplane, c_proper;
  326.  
  327. /*
  328. ================
  329. SingleLightFace
  330. ================
  331. */
  332. void SingleLightFace (entity_t *light, lightinfo_t *l)
  333. {
  334.     double    dist;
  335.     vec3_t    incoming;
  336.     double    angle;
  337.     double    add;
  338.     double    *surf;
  339.     qboolean    hit;
  340.     int        mapnum;
  341.     int        size;
  342.     int        c, i;
  343.     vec3_t    rel;
  344.     vec3_t    spotvec;
  345.     double    falloff;
  346.     double    *lightsamp;
  347.     
  348.     VectorSubtract (light->origin, bsp_origin, rel);
  349.     dist = scaledist * (DotProduct (rel, l->facenormal) - l->facedist);
  350.     
  351. /* don't bother with lights behind the surface */
  352.     if (dist <= 0)
  353.         return;
  354.         
  355. /* don't bother with light too far away */
  356.     if (dist > light->light)
  357.     {
  358.         c_culldistplane++;
  359.         return;
  360.     }
  361.  
  362.     if (light->targetent)
  363.     {
  364.         VectorSubtract (light->targetent->origin, light->origin, spotvec);
  365.         VectorNormalize (spotvec);
  366.         if (!light->angle)
  367.             falloff = -cos(20*Q_PI/180);    
  368.         else
  369.             falloff = -cos(light->angle/2*Q_PI/180);
  370.     }
  371.     else
  372.         falloff = 0;    /* shut up compiler warnings */
  373.     
  374.     mapnum = 0;
  375.     for (mapnum=0 ; mapnum<l->numlightstyles ; mapnum++)
  376.         if (l->lightstyles[mapnum] == light->style)
  377.             break;
  378.     lightsamp = l->lightmaps[mapnum];
  379.     if (mapnum == l->numlightstyles)
  380.     {    /* init a new light map */
  381.         if (mapnum == MAXLIGHTMAPS)
  382.         {
  383.             ShowWarningEntry("Too many light styles on a face\n");
  384.             sleep(1);
  385.                         return;
  386.         }
  387.         size = (l->texsize[1]+1)*(l->texsize[0]+1);
  388.         for (i=0 ; i<size ; i++)
  389.             lightsamp[i] = 0;
  390.     }
  391.  
  392. /* */
  393. /* check it for real */
  394. /* */
  395.     hit = false;
  396.     c_proper++;
  397.     
  398.     surf = l->surfpt[0];
  399.     for (c=0 ; c<l->numsurfpt ; c++, surf+=3)
  400.     {
  401.         dist = CastRay(light->origin, surf)*scaledist;
  402.         if (dist < 0)
  403.             continue;    /* light doesn't reach */
  404.  
  405.         VectorSubtract (light->origin, surf, incoming);
  406.         VectorNormalize (incoming);
  407.         angle = DotProduct (incoming, l->facenormal);
  408.         if (light->targetent)
  409.         {    /* spotlight cutoff */
  410.             if (DotProduct (spotvec, incoming) > falloff)
  411.                 continue;
  412.         }
  413.  
  414.         angle = (1.0-scalecos) + scalecos*angle;
  415.         add = light->light - dist;
  416.         add *= angle;
  417.         if (add < 0)
  418.             continue;
  419.  
  420.         lightsamp[c] += add;
  421.         if (lightsamp[c] > 1)        /* ignore real tiny lights */
  422.             hit = true;
  423.     }
  424.     if (mapnum == l->numlightstyles && hit)
  425.     {
  426.         l->lightstyles[mapnum] = light->style;
  427.         l->numlightstyles++;    /* the style has some real data now */
  428.     }
  429. }
  430.  
  431. /*
  432. ============
  433. FixMinlight
  434. ============
  435. */
  436. void FixMinlight (lightinfo_t *l)
  437. {
  438.     int        i, j;
  439.     float    minlight;
  440.     
  441.     minlight = minlights[l->surfnum];
  442.  
  443. /* if minlight is set, there must be a style 0 light map */
  444.     if (!minlight)
  445.         return;
  446.     
  447.     for (i=0 ; i< l->numlightstyles ; i++)
  448.     {
  449.         if (l->lightstyles[i] == 0)
  450.             break;
  451.     }
  452.     if (i == l->numlightstyles)
  453.     {
  454.         if (l->numlightstyles == MAXLIGHTMAPS)
  455.             return;        /* oh well.. */
  456.         for (j=0 ; j<l->numsurfpt ; j++)
  457.             l->lightmaps[i][j] = minlight;
  458.         l->lightstyles[i] = 0;
  459.         l->numlightstyles++;
  460.     }
  461.     else
  462.     {
  463.         for (j=0 ; j<l->numsurfpt ; j++)
  464.             if ( l->lightmaps[i][j] < minlight)
  465.                 l->lightmaps[i][j] = minlight;
  466.     }
  467. }
  468.  
  469.  
  470. /*
  471. ============
  472. LightFace
  473. ============
  474. */
  475. void LightFace (int surfnum)
  476. {
  477.     dface_t *f;
  478.     lightinfo_t    l;
  479.     int        s, t;
  480.     int        i,j,c;
  481.     double    total;
  482.     int        size;
  483.     int        lightmapwidth, lightmapsize;
  484.     byte    *out;
  485.     double    *light;
  486.     int        w, h;
  487.     
  488.     f = dfaces + surfnum;
  489.  
  490.     facebrightness = 0;
  491.     facebrightnesscount = 0;
  492.  
  493. /* */
  494. /* some surfaces don't need lightmaps */
  495. /* */
  496.     f->lightofs = -1;
  497.     for (j=0 ; j<MAXLIGHTMAPS ; j++)
  498.         f->styles[j] = 255;
  499.  
  500.     if ( texinfo[f->texinfo].flags & TEX_SPECIAL)
  501.     {    /* non-lit texture */
  502.         return;
  503.     }
  504.  
  505.     memset (&l, 0, sizeof(l));
  506.     l.surfnum = surfnum;
  507.     l.face = f;
  508.  
  509. /* */
  510. /* rotate plane */
  511. /* */
  512.     VectorCopy (dplanes[f->planenum].normal, l.facenormal);
  513.     l.facedist = dplanes[f->planenum].dist;
  514.     if (f->side)
  515.     {
  516.         VectorSubtract (vec3_origin, l.facenormal, l.facenormal);
  517.         l.facedist = -l.facedist;
  518.     }
  519.     
  520.     CalcFaceVectors (&l);
  521.     CalcFaceExtents (&l);
  522.     CalcPoints (&l);
  523.  
  524.     lightmapwidth = l.texsize[0]+1;
  525.  
  526.     size = lightmapwidth*(l.texsize[1]+1);
  527.     if (size > SINGLEMAP)
  528.         Error ("Bad lightmap size");
  529.  
  530.     for (i=0 ; i<MAXLIGHTMAPS ; i++)
  531.         l.lightstyles[i] = 255;
  532.     
  533. /* */
  534. /* cast all lights */
  535. /*     */
  536.  
  537.         l.numlightstyles = 0;
  538.     for (i=0 ; i<num_entities ; i++)
  539.         if (entities[i].light)
  540.             SingleLightFace (&entities[i], &l);
  541.  
  542.     FixMinlight (&l);
  543.  
  544.     SetForeColor(ANSI_WHITE);
  545.         SetBackColor(ANSI_BLUE);
  546.         MoveCurs(1,1);
  547.     CPrintf(" ");
  548.                 
  549.     if (!l.numlightstyles)
  550.     {    /* no light hitting it */
  551.         return;
  552.     }
  553.     
  554. /* */
  555. /* save out the values */
  556. /* */
  557.     for (i=0 ; i <MAXLIGHTMAPS ; i++)
  558.         f->styles[i] = l.lightstyles[i];
  559.  
  560.     lightmapsize = size*l.numlightstyles;
  561.  
  562.     out = GetFileSpace (lightmapsize);
  563.     f->lightofs = out - filebase;
  564.     
  565. /* extra filtering */
  566.     h = (l.texsize[1]+1)*2;
  567.     w = (l.texsize[0]+1)*2;
  568.  
  569.     for (i=0 ; i< l.numlightstyles ; i++)
  570.     {
  571.         if (l.lightstyles[i] == 0xff)
  572.             Error ("Wrote empty lightmap");
  573.         light = l.lightmaps[i];
  574.         c = 0;
  575.         for (t=0 ; t<=l.texsize[1] ; t++)
  576.             for (s=0 ; s<=l.texsize[0] ; s++, c++)
  577.             {
  578.                 if (extrasamples)
  579.                 {    /* filtered sample */
  580.                     total = light[t*2*w+s*2] + light[t*2*w+s*2+1]
  581.                     + light[(t*2+1)*w+s*2] + light[(t*2+1)*w+s*2+1];
  582.                     total *= 0.25;
  583.                 }
  584.                 else total = light[c];
  585.                 if (total > 255) total = 255;
  586.                 if (total < 0) total = 0;
  587.                 *out++ = total;
  588.  
  589.                 facebrightness += total;
  590.                 facebrightnesscount++;
  591.                         }
  592.     }
  593.  
  594.     facebrightness /= facebrightnesscount;
  595. }
  596.  
  597.